class_name BallStateCarried
extends BallState

const dribble_frequency:float = 10
const dribble_insensity:float = 3
const OFFSET_FROM_PLAYER : Vector2 = Vector2(10,4)
var dribble_time:=0.0

func _enter_tree() -> void:
	assert(carrier != null,"carrier is not exists")
	ball.height = 0.0
	GameEvents.ball_possessed.emit(carrier.full_name)
	
func _process(delta: float) -> void:
	var vx := 0
	dribble_time += delta
	if carrier.velocity.x != 0:
		vx = cos(dribble_time * dribble_frequency) * dribble_insensity
		
	if carrier.velocity != Vector2.ZERO:
		if carrier.heading.x >= 0:
			animation_player.play("roll")
		else:
			animation_player.play("roll",-1,-1)
	else:
		animation_player.play("idle")
		
	ball.position = carrier.position + Vector2(vx + carrier.heading.x * OFFSET_FROM_PLAYER.x,OFFSET_FROM_PLAYER.y)

func _exit_tree() -> void:
	GameEvents.ball_released.emit()
